﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Spring;
using Cn.Cellula.Biz.Port;
using Cn.Cellula.Model;
using System.Drawing;
using System.Collections.Generic;
using Cn.Cellula.Client.ControlHelper;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Model.System;
using Cn.Cellula.Client;

namespace Cn.Cellula.Website.Admin.Content.Code {
    public partial class TypeList : PageSupport {
        ICodeBiz biz = BizFactory.Get<ICodeBiz>();
        String TypeKey = null;
        protected void Page_Load(object sender, EventArgs e) {
            TypeKey = Request.QueryString["t"];
            if (!Page.IsPostBack) {
                LoadTypeList();
            }
        }

        private void LoadTypeList() {
            IList<TSysCodeTypeEO> list = biz.LoadAll<TSysCodeTypeEO>();
            this.gvlist.DataSource = list;
            this.gvlist.DataBind();
            if (String.IsNullOrEmpty(TypeKey)) {
                if (list != null && list.Count > 0) {
                    LoadCodeList(list[0].TypeKey);
                    GvHelper.GvSelected(ref this.gvlist, list[0].TypeKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
                } else {
                    this.btn_value_add.Enabled = false;
                    this.btn_value_delete.Enabled = false;
                    this.btn_value_update.Enabled = false;
                }
            } else {
                LoadCodeList(TypeKey);
                GvHelper.GvSelected(ref this.gvlist, TypeKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }


        private void LoadCodeList(String rTypeKey) {
            this.hid_SelectedTypeKey.Value = rTypeKey;
            try {
                TSysCodeTypeEO teo = biz.Load<TSysCodeTypeEO>(rTypeKey);
                if (teo.IsLocked == 1) {
                    this.btn_value_add.Enabled = false;
                    this.btn_value_delete.Enabled = false;
                    this.btn_value_update.Enabled = false;
                } else {
                    this.btn_value_add.Enabled = true;
                    this.btn_value_delete.Enabled = true;
                    this.btn_value_update.Enabled = true;
                }
                this.gvCodeList.DataSource = biz.LoadCodeByType(rTypeKey);
                this.gvCodeList.DataBind();
            } catch { }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvlist_PageIndexChanging(object sender, GridViewPageEventArgs e) {
            gvlist.PageIndex = e.NewPageIndex;
            LoadTypeList();
        }

        protected void gvlist_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                //String key = gvlist.DataKeys[e.Row.RowIndex].Values[0].ToString();
                //string url = "Admin/Content/Code/_CodeList.aspx";
                //e.Row.Attributes.Add("url", url + "?ID=" + key);
                //e.Row.Attributes.Add("onclick", "return myUtil.onGridViewRowSelected(this,event,'cntrlist_container');");
            }
        }

        protected void btn_type_add_Click(object sender, EventArgs e) {
            Response.Redirect("TypeEdit.aspx");
        }

        protected void btn_type_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("TypeEdit.aspx?ID=" + keys[0] + "");
            }
        }

        protected void btn_type_delete_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            biz.DeleteCodeType(keys);
            BizFactory.Get<ILogBiz>().LogDelete("代码类型", keys.Count, this.Actor, Request);
            LoadTypeList();
        }

        protected void btn_value_add_Click(object sender, EventArgs e) {
            Response.Redirect("CodeEdit.aspx?t="+this.hid_SelectedTypeKey.Value);
        }

        protected void btn_value_update_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvCodeList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                Response.Redirect("CodeEdit.aspx?ID=" + keys[0] + "");
            }
        }

        protected void btn_value_delete_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvCodeList, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            biz.DeleteCodeValue(keys);
            BizFactory.Get<ILogBiz>().LogDelete("代码值", keys.Count, this.Actor, Request);
            this.LoadCodeList(this.hid_SelectedTypeKey.Value);
        }

        protected void gvlist_SelectedIndexChanged(object sender, EventArgs e) {
        }

        protected void gvlist_RowCommand(object sender, GridViewCommandEventArgs e) {
            if (e.CommandName == "CheckSubList") {
                String typeKey = e.CommandArgument.ToString();
                this.hid_SelectedTypeKey.Value = typeKey;
                this.LoadCodeList(typeKey);
                GvHelper.GvSelected(ref this.gvlist, typeKey, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }

        protected void btn_Lock_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                biz.SetTypeLock(keys, true);
                LoadTypeList();
                GvHelper.GvSelected(ref this.gvlist, keys[0], ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }

        protected void btn_UnLock_Click(object sender, EventArgs e) {
            IList<String> keys = GvHelper.GetSelectedKey(ref this.gvlist, ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            if (keys.Count > 0) {
                biz.SetTypeLock(keys, false);
                LoadTypeList();
                GvHelper.GvSelected(ref this.gvlist, keys[0], ClientBiz.GRIDVIEW_ROW_CHECKBOX_ID);
            }
        }
    }
}
